package session.stateless.people;

import entity.people.PeopleEntity;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless
public class PeopleSession implements PeopleSessionLocal {
    @PersistenceContext()
    EntityManager em;
    
    @Override
    public void createPeople(String firstName, String lastName, String gender, String address, String city, String country, String phone1, String phone2, String email1, String email2, Date dateOfBirth, Timestamp dateAdded, String passport, String type) {
        PeopleEntity people = new PeopleEntity();
        
        people.setFirstName(firstName);
        people.setLastName(lastName);
        people.setGender(gender);
        people.setAddress(address);
        people.setCity(city);
        people.setCountry(country);
        people.setPhone1(phone1);
        people.setPhone2(phone2);
        people.setEmail1(email1);
        people.setEmail2(email2);
        people.setPassport(passport);
        people.setDateOfBirth(dateOfBirth);
        people.setDateAdded(dateAdded);
        people.setType(type);
        
        em.persist(people);
    }
    
    @Override
    public void editPeople(String address, String city, String country, String phone1, String phone2, String email1, String email2, String passport, String type){
        PeopleEntity people =em.find(PeopleEntity.class, passport);
        
        if (people ==null)
            System.out.println("People does not exist.");
        else
        {
            people.setAddress(address);
            people.setCity(city);
            people.setCountry(country);
            people.setEmail1(email1);
            people.setEmail2(email2);
            people.setPhone1(phone1);
            people.setPhone2(phone2);
            people.setPassport(passport);
            people.setType(type);
            
            em.flush();                        
        }
    }
    
    @Override
    public void deletePeople(String passport){
        PeopleEntity people = em.find(PeopleEntity.class, passport);
        if (people!=null) em.flush();
    }
    
    @Override
    public List<PeopleEntity> findAll() {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p");
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findId(Long id) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.id = :id");
        q.setParameter("id", id);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findFirstName(String firstName) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.firstName = :firstName");
        q.setParameter("firstName", firstName);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findLastName(String lastName) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.lastName = :lastName");
        q.setParameter("lastName", lastName);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findGender(String gender) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.gender = :gender");
        q.setParameter("gender", gender);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findCity(String city) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.city = :city");
        q.setParameter("city", city);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findCountry(String country) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.country = :country");
        q.setParameter("country", country);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findPhone1(String phone1) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.phone1 = :phone1");
        q.setParameter("phone1", phone1);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findPhone2(String phone2) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.phone2 = :phone2");
        q.setParameter("phone2", phone2);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findEmail1(String email1) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.email1 = :email1");
        q.setParameter("email1", email1);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findEmail2(String email2) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.email2 = :email2");
        q.setParameter("email2", email2);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findPassport(String passport) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.passport = :passport");
        q.setParameter("passport", passport);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findDateOfBirth(Date dateOfBirth){
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.dateOfBirth = :dateOfBirth");
        q.setParameter("dateOfBirth", dateOfBirth);
        return q.getResultList();
    }
    
    @Override
    public List<PeopleEntity> findType(String type) {
        Query q=em.createQuery("SELECT p FROM PeopleEntity p WHERE p.type = :type");
        q.setParameter("type", type);
        return q.getResultList();
    }
    
}
